package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.BillConsumeDetailsEntity;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created By Dlx
 * on 2019/6/29
 */
public class ADA_BillGoodsDetail extends CommonAdapter<BillConsumeDetailsEntity.GoodsListBean> {
    private Context mContext;

    private boolean isIntegralGoods = false;

    public ADA_BillGoodsDetail(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_bill_detail_consumption;
    }

    @Override
    protected void convert(ViewHolder holder, BillConsumeDetailsEntity.GoodsListBean entity, int position) {
        bindGoodsData(holder, entity, position);
    }

    private void bindGoodsData(ViewHolder holder, BillConsumeDetailsEntity.GoodsListBean entity, int position) {
        holder.setSpanText(R.id.goods_name_tv, addGoodsItemLabel(entity, (TextView) holder.getView(R.id.goods_name_tv)));
        holder.setText(R.id.goods_num_tv, "购买数量：" + entity.getGoodsNum());

        if (!isIntegralGoods) {
            holder.setText(R.id.goods_real_price_tv, "￥" + (StringUtils.isEmpty(entity.getRealPay()) ? entity.getPrice() : entity.getRealPay()));
            holder.setSpanText(R.id.goods_price_tv, entity.getPrice().equals(entity.getRealPay()) ? new SpannableStringBuilder() :
                    StringUtils.isEmpty(entity.getRealPay()) ? new SpannableStringBuilder() : getDeleteLineText(entity.getPrice()));
        } else {
            holder.setText(R.id.goods_real_price_tv, String.format("%s积分", BigDecimalUtil.getNumber(entity.getIntegral())));
            holder.setText(R.id.goods_price_tv, "");
        }

        holder.setText(R.id.goods_return_num_tv, StringUtils.isTrue(entity.getIsReturn()) ? "退货数量：" + entity.getReturnNum() : "");

        StringBuffer specStr = new StringBuffer();
        specStr.append("规格：");
        specStr.append(entity.getGoodsSpec());
        if (!TextUtils.isEmpty(entity.getGoodsSpec())) {
            specStr.append(",");
        }
        specStr.append(entity.getUnitName());

        holder.setText(R.id.goods_unit_tv, specStr.toString());
    }

    public void setIntegralGoods(boolean integralGoods) {
        isIntegralGoods = integralGoods;
    }

    private SpannableStringBuilder getDeleteLineText(String str) {
        return StringSpanUtils.init().addForegroundColorHighLight(mContext.getResources().getColor(R.color.order_status_close), "￥" + str)
                .setDeleteLineWithKeyAndPosition("￥" + str, 0)
                .build();
    }

    private SpannableStringBuilder addGoodsItemLabel(BillConsumeDetailsEntity.GoodsListBean entity, TextView textView) {
        StringSpanUtils utils = StringSpanUtils.init().addStr(entity.getGoodsName());
        if (!StringUtils.isEmpty(entity.getBargainingPrice()) && !entity.getBargainingPrice().equals("0.00")) {
            utils.addRoundRectLabelWithRadius("议价", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }else if (!StringUtils.isEmpty(entity.getSecondPrice()) && !entity.getSecondPrice().equals("0.00")) {
            utils.addRoundRectLabelWithRadius("变价", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }

        if (!StringUtils.isEmpty(entity.getPresentNum()) && !"0".equals(entity.getPresentNum())) {
            utils.addRoundRectLabelWithRadius("赠送(" + entity.getPresentNum() + ")", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }
        if (StringUtils.isTrue(entity.getIsConsign())) {
            utils.addRoundRectLabelWithRadius("寄存(" + entity.getConsignNum() + ")", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }
//        if (StringUtils.isTrue(entity.getIsReturn())) {
//            RTextView label = (RTextView) LayoutInflater.from(mContext).inflate(R.layout.item_bill_goods_label, null);
//            label.setText("退货");
//            layout.addView(label);
//        }
        return utils.build();
    }

}
